package com.vdong.trade.trading.web.controller;

import com.vdong.trade.trading.common.contract.ApiContract;
import com.vdong.trade.trading.common.exception.ProcessException;
import com.vdong.trade.trading.common.foundation.FoundationController;
import com.vdong.trade.trading.entity.po.FinanceParamDTO;
import com.vdong.trade.trading.entity.vo.UserAmountVO;
import com.vdong.trade.trading.facade.enums.UserTypeEnum;
import com.vdong.trade.trading.service.AmountService;
import lombok.extern.slf4j.Slf4j;
import org.lamb.framework.common.userDTO.AccountSystemUserInfoDTO;
import org.lamb.framework.common.userDTO.PeddleUserDTO;
import org.lamb.framework.common.util.CopyUtil;
import org.lamb.framework.common.util.StringUtil;
import org.lamb.framework.core.security.LambPrincipalFactoryContainer;
import org.lamb.framework.core.templete.LambResponseTemplete;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import reactor.core.publisher.Mono;

import javax.annotation.Resource;

import static com.vdong.trade.trading.common.enums.ProcessExceptionEnum.EB00000005;
import static com.vdong.trade.trading.common.enums.ProcessExceptionEnum.EB00000008;

/**
 * 用户资金
 *
 * @author zhoulongbiao
 * @Date 2018/12/22 11:10
 */
@RestController
@CrossOrigin
@Slf4j
public class AmountController extends FoundationController {

    @Resource
    private AmountService amountService;

    /**
     * 用户资金
     */
    @RequestMapping(value = ApiContract.I000000020, method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
    public Mono<LambResponseTemplete> earnings(@RequestBody FinanceParamDTO param) {
        if (StringUtil.hasBlank(param.getUserId(), param.getUserType()))
            return Mono.error(new ProcessException(EB00000008));

        if (StringUtil.hasBlank(param.getUserType())) {
            return Mono.error(new ProcessException(EB00000008));
        }

        if (param.getUserType().equals(UserTypeEnum.COMPANY.getValue().toString()) ||
                param.getUserType().equals(UserTypeEnum.MERCHANT.getValue().toString())) {
            // 获取当前商户/saas用户缓存信息
            AccountSystemUserInfoDTO accountSystemUser = LambPrincipalFactoryContainer.getAccountSystemUser();
            if (accountSystemUser == null) {
                log.error("商户提现获取用户缓存信息失败!");
                return Mono.error(new ProcessException(EB00000005));
            }
            param.setUserId(accountSystemUser.getSysUserId());
        } else if (param.getUserType().equals(UserTypeEnum.PUSHER.getValue().toString())) {
            // 获取当前推客用户缓存信息
            PeddleUserDTO peddleUser = LambPrincipalFactoryContainer.getPeddleUser();
            if (peddleUser == null) {
                log.error("推客提现获取用户缓存信息失败!");
                return Mono.error(new ProcessException(EB00000005));
            }
            param.setUserId(String.valueOf(peddleUser.getId()));
        } else {
            return Mono.error(new ProcessException(EB00000008));
        }

        return returning(CopyUtil.copyObject(amountService.selectAmountMOByUserId(Long.valueOf(param.getUserId()),
                param.getUserType()), new UserAmountVO()));
    }

    public static void main(String[] args) {
        if (UserTypeEnum.COMPANY.getValue().toString().equals("1")) {
            System.out.println("1");
        } else if (UserTypeEnum.MERCHANT.getValue().toString().equals("2")) {
            System.out.println("2");
        }
    }
}
